package cn.wl.crm.web.controller;

import cn.wl.AjaxResult;
import cn.wl.PageList;
import cn.wl.crm.domain.RepairOrderItem;
import cn.wl.crm.query.RepairOrderItemQuery;
import cn.wl.crm.service.IRepairOrderItemService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/repairOrderItem")
@Api(tags = "用户接口类")
@CrossOrigin
public class RepairOrderItemController {
    @Autowired
    private IRepairOrderItemService repairOrderItemService;

    @RequestMapping(value = "/list",method = RequestMethod.PATCH)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=500,message="服务器错误")
    })
    public List<RepairOrderItem> queryAll(){
        return repairOrderItemService.queryAll();

    }
    @RequestMapping(value = "/getAmt/{id}",method = RequestMethod.GET)
    @ApiOperation(value="根据ID查询用户",notes = "getOne查询方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id参数",required = true,dataType = "Long",paramType = "path")
    })
    @ResponseBody
    public BigDecimal getAmt(@PathVariable Long id){
        return repairOrderItemService.findAmtByOrder(id);
    }

    //分页查询方法
    @RequestMapping(value = "/queryPage",method = RequestMethod.PATCH)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=500,message="服务器错误")
    })
    @CrossOrigin
    public PageList queryPage(@RequestBody RepairOrderItemQuery repairOrderItemQuery){
        return repairOrderItemService.queryPage(repairOrderItemQuery);
    }
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ApiOperation(value="根据ID查询用户",notes = "getOne查询方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id参数",required = true,dataType = "Long",paramType = "path")
    })
    @ResponseBody
    public RepairOrderItem getOne(@PathVariable Long id){
        return repairOrderItemService.queryOne(id);
    }

    @RequestMapping(value = "/save",method = RequestMethod.PUT)
    @ResponseBody
    public AjaxResult save(@RequestBody RepairOrderItem repairOrderItem){
        try {
            Long num = repairOrderItem.getNum();
            BigDecimal amt = repairOrderItem.getAmt2();
            for(int i=0;i<num;i++){
                amt=amt.add(repairOrderItem.getAmt1());
            }
            repairOrderItem.setTotalAmt(amt);
            repairOrderItemService.save(repairOrderItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("操作失败！！！");
        }
        return new AjaxResult();
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult update(@RequestBody RepairOrderItem repairOrderItem){
        try {
            repairOrderItemService.update(repairOrderItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("操作失败！！！");
        }
        return new AjaxResult();
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public AjaxResult update(@PathVariable Long id){
        try {
            repairOrderItemService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("操作失败！！！");
        }
        return new AjaxResult();
    }
}
